<template>
  <div class="root">
    <div class="left-container">
      <div class="title-container">
        <img src="@/assets/images/icon.png" width="40" height="40" />
        <span class="title-text">课题审批系统</span>
      </div>
      <el-menu
        background-color="#343844"
        class="el-menu-container"
        :default-active="defaultActive"
        text-color="#fff"
        @select="handleSelect"
        unique-opened
      >
        <!-- 流程管理菜单，权限为 admin 才显示 -->
        <el-sub-menu index="/workflow" v-if="hasPermission('admin')">
          <template #title>
            <el-icon>
              <Connection />
            </el-icon>
            <span>流程管理</span>
          </template>
          <el-menu-item index="/workflow/definition">
            <el-icon>
              <Tickets />
            </el-icon>
            <span>流程定义</span>
          </el-menu-item>
          <el-menu-item index="/workflow/instances">
            <el-icon>
              <List />
            </el-icon>
            <span>流程实例管理</span>
          </el-menu-item>
          <el-menu-item index="/workflow/listener">
            <el-icon>
              <Operation />
            </el-icon>
            <span>服务</span>
          </el-menu-item>
          <el-menu-item index="/workflow/report">
            <el-icon>
              <Operation />
            </el-icon>
            <span>报表分析</span>
          </el-menu-item>
        </el-sub-menu>

        <!-- 与我有关菜单 -->
        <el-sub-menu index="/my">
          <template #title>
            <el-icon><Position /></el-icon>
            <span>与我有关</span>
          </template>
          <el-menu-item index="/my/start">
            <el-icon><Odometer /></el-icon>
            <span>课题申请</span>
          </el-menu-item>
          <el-menu-item index="/my/tasks">
            <el-icon><ChatLineRound /></el-icon>
            <span>任务管理</span>
          </el-menu-item>
        </el-sub-menu>

        <!-- 系统管理菜单，权限为 admin 才显示 -->
        <el-sub-menu index="/sys" v-if="hasPermission('admin')">
          <template #title>
            <el-icon>
              <SetUp />
            </el-icon>
            <span>系统管理</span>
          </template>
          <el-menu-item index="/sys/user">
            <el-icon>
              <User />
            </el-icon>
            <span>账号信息</span>
          </el-menu-item>
          <el-menu-item index="/sys/dept">
            <el-icon>
              <Operation />
            </el-icon>
            <span>部门信息</span>
          </el-menu-item>

          <el-menu-item index="/HorizontalProjectTable">
            <span>横向课题汇总表</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </div>

    <div class="right-container">
      <div class="head-container">
        <span class="menu-name">欢迎登录！今天是{{ dateVal }}</span>
        <div class="head-user-container">
          <div>{{ user.account }}</div>
          <img class="head-user-close" src="../assets/images/close.png" @click="logout" />
        </div>
      </div>

      <div class="page">
        <div class="router-container">
          <router-view />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import Cookies from "js-cookie"; // 引入 Cookies 库
import { getCache, removeCache } from "@/utils/cache";
import { CacheToken } from "@/constants/cacheKey";

const router = useRouter();
let defaultActive = ref("/workflow/definition");

// 定义 user 对象类型
const user = ref<{
  account: string;
  permissions: string;
}>({
  account: "",
  permissions: ""
});

let dateVal = ref<string>("");

/**
 * 初始化
 */
const onInit = () => {
  const userInfo = getCache(CacheToken);
  user.value = userInfo;
  const permissions = Cookies.get("permissions"); // 从 Cookie 获取权限
  if (permissions) {
    user.value.permissions = permissions; // 设置用户权限
  }
  defaultActive.value = router.currentRoute.value.path; // 设置当前活动的菜单路径
};

/**
 * 退出登录
 */
const logout = () => {
  removeCache(CacheToken); // 清除缓存中的 token
  router.replace("/login"); // 跳转到登录页面
};

/**
 * 菜单选择
 * @param key 路由
 */
const handleSelect = (key: string) => {
  defaultActive.value = key; // 更新默认激活菜单
  router.push(key); // 跳转到选择的路由
};

/**
 * 获取时间
 */
const getDate = () => {
  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const week = "日一二三四五六".charAt(new Date().getDay());
  dateVal.value = `${year}年${getNum(month)}月${getNum(day)}日 星期${week}`;
};

/**
 * 如果是单数字则前面加0
 * @param num 数字
 * @returns 返回格式化的两位数
 */
const getNum = (num: number) => {
  return num < 10 ? "0" + num : num.toString();
};

/**
 * 判断是否有权限
 * @param permission 权限字符串
 * @returns true/false
 */
const hasPermission = (permission: string) => {
  return user.value.permissions === permission;
};

// 组件挂载时执行
onMounted(() => {
  getDate(); // 获取当前日期
  onInit();  // 初始化用户信息和权限
});
</script>


<style scoped>
.root {
  display: flex;
  height: 100vh;
  width: 100%;
}

.left-container {
  width: 250px !important;
  background: #343844;
  min-width: 250px !important;
}

.right-container {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}

.title-container {
  background: #343844;
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: solid 1px white;
}

.title-text {
  margin-left: 10px;
  font-family: "幼圆";
  font-size: 23px;
  font-weight: 900;
  color: white;
}

.el-menu-container {
  height: calc(100vh - 150px);
}

/* 针对一级菜单激活样式 */
.el-menu-container :deep(.el-sub-menu > .el-menu-item.is-active) {
  background: #fcbc02;
  border-radius: 0 80px 80px 0;
  color: #532f00;
}

/* 针对二级菜单激活样式 */
.el-menu-container :deep(.el-menu-item.is-active) {
  background: #fcbc02;
  border-radius: 0 80px 80px 0;
  color: #532f00;
}

.head-container {
  height: 80px;
  display: flex;
  width: 100%;
  align-items: center;
  background: white;
  border: solid 1px #e8e8e8;
}

.menu-name {
  font-size: 20px;
  font-weight: 600;
  color: #000000;
  margin-left: 20px;
}

.head-user-container {
  position: absolute;
  right: 20px;
  font-size: 16px;
  font-weight: 500;
  color: #000000;
  display: flex;
  justify-content: center;
  align-items: center;
}

.head-user-close {
  width: 30px;
  height: 30px;
  margin-left: 10px;
  cursor: pointer;
}

.page {
  padding: 10px;
  height: calc(100vh - 80px);
  overflow-y: auto;
  background: #f0f2f5;
}

.router-container {
  background-color: white;
  padding: 20px;
}
</style>
